package it.gtd.swimpool.model.entity;

import com.google.common.annotations.Beta;

import javax.persistence.Embeddable;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.io.Serializable;
import java.util.Date;

import static com.google.common.base.Preconditions.checkNotNull;

@Embeddable
@Beta
class MessageId implements Serializable {
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;

    @ManyToOne
    private User sender;

    @ManyToOne
    private User receiver;

    public MessageId() {
        this.date = new Date();
    }

    public Date getDate() {
        return date;
    }

    public User getSender() {
        return sender;
    }

    public void setSender(User sender) {
        checkNotNull(sender, "sender can't be null");

        this.sender = sender;
    }

    public User getReceiver() {
        return receiver;
    }

    public void setReceiver(User receiver) {
        checkNotNull(receiver, "receiver can't be null");

        this.receiver = receiver;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MessageId messageId = (MessageId) o;

        if (!date.equals(messageId.date)) return false;
        if (!receiver.equals(messageId.receiver)) return false;
        if (!sender.equals(messageId.sender)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = date.hashCode();
        result = 31 * result + sender.hashCode();
        result = 31 * result + receiver.hashCode();
        return result;
    }
}
